import { rmSync } from "node:fs";
import { defineConfig } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import mockServer from 'vite-plugin-mock-server'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig((command) => {
   rmSync("dist-electron", { recursive: true, force: true });

  return {
    envDir: path.resolve(__dirname, "./env"),//环境配置
    resolve: { // 别名配置
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@store": path.resolve(__dirname, "./src/store/modules"),
        "@api": path.resolve(__dirname, "./src/api"),
        "@globel": path.resolve(__dirname, "./globel"),
      },
    },
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(__dirname, "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",// 注意这里的icon- 前缀我在svgIcon.vue中写死了的，如果调整了记得同步改一下
        // 有特殊需求可不进行该配置
        svgoOptions: {
          // 删除一些填充的属性
          plugins: [
            {
              name: "removeAttrs",
              params: { attrs: ["class", "data-name", "fill", "stroke"] },
            },
            // 删除样式标签
            "removeStyleElement",
          ],
        },
      }),
      electron({
        main: {
          // Shortcut of `build.lib.entry`.
          entry: 'electron/main.ts'
        },
        preload: {
          // Shortcut of `build.rollupOptions.input`.
          // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
          input: path.join(__dirname, 'electron/preload.ts'),
        },
        // Ployfill the Electron and Node.js API for Renderer process.
        // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
        // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
        renderer: process.env.NODE_ENV === 'test'
          // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
          ? undefined
          : {},

      }),
      AutoImport({
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件 
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
        dts: path.resolve(__dirname, 'types/auto-imports.d.ts'),
      }),
      Components({
        resolvers: [
          ElementPlusResolver(),
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          }),
        ],
        dts: path.resolve(__dirname, 'types/components.d.ts'),
      }),
      //补充一个图标的导入配置
      Icons({
        autoInstall: true,
      }),
      // mock数据
      mockServer({
        logLevel: 'info',
        urlPrefixes: ['/api'],
        mockRootDir: path.relative(__dirname, './src/api/mock'),// 模拟数据文件所放置的目录
        mockJsSuffix: '.mock.js', // js模拟数据文件的后缀
        mockTsSuffix: '.mock.ts',// js模拟数据文件的后缀
        noHandlerResponse404: true,
      })
    ],
    clearScreen:false
  };

})
